﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Media;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
//
using ScheduleMVVM.ViewModel;
using ScheduleMVVM.Model.RuntimeFramework;
using GalaSoft.MvvmLight.Messaging;

namespace ScheduleMVVM
{
    /// <summary>
    /// Description for RoutePage.
    /// </summary>
    public partial class RoutePage : PhoneApplicationPage
    {
        /// <summary>
        /// Initializes a new instance of the RoutePage class.
        /// </summary>
        public RoutePage()
        {
            InitializeComponent();
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            var query = NavigationContext.QueryString;
            if (query.Count != 0) // User has pressed on a secondary tile
            {
                var route = AdvancedRuntimeContext.Current.GetFavouriteEntry(
                    Int32.Parse(query["start"]),
                    Int32.Parse(query["end"]));
                Messenger.Default.Send<UpdateRouteViewCommand>(new UpdateRouteViewCommand(new RouteEntryWrapper(route)));
            }
        }

        private void PhoneApplicationPage_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            SystemTray.SetIsVisible(this, true);
            SystemTray.SetProgressIndicator(this, progressBar);

            // Implement navigation fix
            var queue = NavigationService.BackStack;
            var iterator = queue.GetEnumerator();
            if (!iterator.MoveNext()) return;
            if (iterator.Current.Source.OriginalString.Contains("SearchPage")) NavigationService.RemoveBackEntry();
        }

    }
}